/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package heartinjava;

import javax.swing.JOptionPane;

/**
 *
 * @author TIENDAT
 */
public final class HeartInJava implements Runnable {

    private int nState;
    private KhoiTao[] gameState;
    private int currentState;
    private KhungBanCo gui;
    private int type;
    public Thread thread;
    private String playerID;
    private int viTri;
    private Server server;
    private Client client;
    
    public HeartInJava() 
    {
        gui = new KhungBanCo(this);
        nState = 10;
        gameState = new KhoiTao[nState];
        gameState[KhaiBao.MENU] = new Menu(this,gui);
        gameState[KhaiBao.CONNECTCLIENT] = new ConnectClient(this, gui);
        gameState[KhaiBao.CONNECTSERVER] = new ConnectServer(this, gui);
        
        gameState[KhaiBao.WAIT] = new XLWaiting(this,gui);
        SwitchState(KhaiBao.MENU);

        type = -1;
        server = null;
        client = null;
        thread = new Thread(this);
        thread.start();
    }
    
    public int getType() 
    {
        return type;
    }

    public void setType(int type) 
    {
        this.type = type;
    }

    public Thread getThread() 
    {
        return thread;
    }

    public void setThread(Thread thread) 
    {
        this.thread = thread;
    }

    public String getPlayerID() 
    {
        return playerID;
    }

    public void setPlayerID(String playerID) 
    {
        this.playerID = playerID;
    }

    public int getViTri() 
    {
        return viTri;
    }

    public void setViTri(int viTri) 
    {
        this.viTri = viTri;
    }
    
    public Server getServer() 
    {
        return server;
    }

    public void setServer(Server server) 
    {
        this.server = server;
    }

    public Client getClient() 
    {
        return client;
    }

    public void setClient(Client client) 
    {
        this.client = client;
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        try {//com.sun.java.swing.plaf.windows.WindowsLookAndFeel
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                //if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    //javax.swing.UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(HeartInJava.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(HeartInJava.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(HeartInJava.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(HeartInJava.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        
        //// ok men
        HeartInJava gameControl = new HeartInJava();
    }
    
    public void run()
    {
        while(true)
        {
            if (this.type == KhaiBao.ISSERVER)
            {
                if((currentState == KhaiBao.PLAY)&& (this.getServer().getClientCount()<3))
                {
                    final JOptionPane jop = new JOptionPane("Notice");
                    jop.setBounds(300, 300, 300, 300);
                    jop.showMessageDialog(jop, "Không đủ 4 người ... ");
                    try {
                        this.getServer().SendToAllClient("exit");
                        this.getServer().stop();
                    }catch (Exception ex){}
                    SwitchState(KhaiBao.MENU);
                }
            }
            else if (this.type == KhaiBao.ISCLIENT)
            {
                if((currentState == KhaiBao.PLAY) && (this.getClient().m_Connect == false))
                {
                    try{
                        this.getClient().stopThread();
                    }catch(Exception ex){}
                    SwitchState(KhaiBao.MENU);
                }
            }
            gameState[currentState].Update();
            try {
                thread.sleep(100);
            } catch (Exception e) {
            }
        }
    }
    
    public void SinglePlay() 
    {
        gameState[KhaiBao.PLAY] = new ComputerPlayer(this,gui);       
        setType(KhaiBao.ISSINGLE);
        SwitchState(KhaiBao.PLAY);
    }
    
    public void SwitchState(int state) 
    {
        currentState = state;
        gameState[currentState].Enter();
    }

    public void IsClient(String IP, int Port) 
    {
        gameState[KhaiBao.PLAY] = new PlayClient(this,gui);
        setType(KhaiBao.ISCLIENT);
        setClient(new Client(IP, Port,this));
    }

    public void IsServer(int portserver) 
    {
        gameState[KhaiBao.PLAY] = new PlayServer(this,gui);
        setType(KhaiBao.ISSERVER);
        setServer(new Server(portserver,this));
    }
    
    public void ThonDiepTuServer(String msg)
    {
        System.out.println("Server send : " + msg);
        gameState[currentState].HaveMessage(msg);
    }

    public void ThongDiepTuClient(String msg)
    {
        System.out.println("Client send : " + msg);
        gameState[currentState].HaveMessage(msg);
    }
}
